package com.qdxwx.web.controller.article;

import com.qdxwx.common.cache.CacheUtils;
import com.qdxwx.common.constants.CacheConstants;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.DateUtils;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKRequest;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.article.ArticleService;
import com.qdxwx.models.article.Article;
import com.qdxwx.models.article.ArticleQuery;
import com.qdxwx.web.controller.BaseController;
import com.qdxwx.web.utils.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 前台文章资讯
 * @author developer
 */
@Controller
@RequestMapping("/article")
public class ArticleController extends BaseController {

	private static Logger logger = LoggerFactory.getLogger(ArticleController.class);
	@Autowired
	private ArticleService articleService;

	/**
	 * 分页查询文章列表
	 */
	@RequestMapping({"/index.html", "/list.html"})
	public String showArticleList(HttpServletRequest request, ModelMap mmap) {

		if (TKRequest.isMobileBrowse(request)){
			int orderBy = TKConvert.toInteger(request.getParameter("orderBy"));
			mmap.put("orderBy", orderBy);
			return getViewPathH5("article/article-list");
		}

		int currentPage = TKConvert.toInteger(request.getParameter("page"));

		ArticleQuery query = new ArticleQuery();
		String searchKey = TKString.searchKeyFilter(request.getParameter("searchKey"));
		query.setTitle(searchKey);
		PageRequest page = new PageRequest();
		page.setCurrentPage(currentPage);
		page.setPageSize(10);
		mmap.put("totalPages", page.getTotalPages(articleService.count(query)));

		List<Article> articleList = articleService.getList(query, page);
		mmap.put("articleList", articleList);
		if (!TKString.isEmpty(searchKey)) {
			mmap.put("searchKey", searchKey);//
		}
		mmap.put("page", page);
		// 查询文章 按评论数多的 排序
		query = new ArticleQuery();
		query.setOrderBy(2);
		List<Article> topArticleList = articleService.getList(query, 5);
		mmap.put("topArticleList", topArticleList);

		List<Article> recommendList = getRecommendList(null);

		mmap.put("recommendList", recommendList);

		return getViewPath("article/article-list");
	}



	/**
	 * 文章详情
	 */
	@RequestMapping("/view/{id}.html")
	public String articleInfo(HttpServletRequest request, @PathVariable("id") int id, ModelMap mmap) {
		// 查询文章详情
		Article article = articleService.get(id);
		if (article == null) {
			return VIEW_ERROR;
		}
		mmap.put("content", article.getContent());
		mmap.put("article", article);
		mmap.put("timeBeforeChn", DateUtils.getBeforeChn(article.getUpdateTime()));
		mmap.put("user", LoginUser.getUser(request));
		articleService.updateViewCount(id);

		if (TKRequest.isMobileBrowse(request)) {
			return getViewPathH5("article/article-view");
		}

		List<Article> recommendList = getRecommendList(null);
		mmap.put("recommendList", recommendList);
		return getViewPath("article/article-view");
	}


	/**
	 * 查询好文推荐（文章按浏览书排行）
	 */
	public List<Article> getRecommendList(ArticleQuery query) {
		@SuppressWarnings("unchecked")
		List<Article> articleList = (List<Article>) CacheUtils.get(CacheConstants.ARTICLE_GOOD_RECOMMEND);
		if (articleList == null || articleList.size() == 0) {
			if (query==null) {
				query = new ArticleQuery();
			}
			query.setOrderBy(1);
			articleList = articleService.getList(query, 10);
			CacheUtils.set(CacheConstants.ARTICLE_GOOD_RECOMMEND, articleList, CacheConstants.RECOMMEND_COURSE_TIME);
		}
		return articleList;
	}
}
